package com.by.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ServivE
 * @version 0.1
 * @className RabbitConfig
 * @description
 * @date 2024/10/23 11:36
 * @since jdk17
 */
@Configuration
public class RabbitConfig {

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadQueue() {
        return new Queue("dead-queue");
    }

    /**
     * 死信交换器
     * @return
     */
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange("deadEx");
    }

    /**
     * 死信队列绑定死信交换器
     * @param deadQueue
     * @param deadExchange
     * @return
     */
    @Bean
    public Binding bindingDead(Queue deadQueue, DirectExchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("order");
    }

    /**
     * 正常队列
     * @return
     */
    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable("order-queue")
                .maxLength(5)//队列长度 最多5条消息 多的进入死信队列
                .overflow(QueueBuilder.Overflow.dropHead)//进入死信队列的消息,默认尾部进入,dropHead:头部进入
                .deadLetterExchange("deadEx")
                //.deadLetterRoutingKey("")//死信路由键,如果正常队列绑定的路由键和死信队列绑定的路由键不一致  需要指定死信绑定的路由键
                .build();
    }

    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normalEx");
    }

    @Bean
    public Binding bindingNormal(Queue normalQueue,DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("order");
    }

}
